package com.mzj.saas.rent;

import com.haixiaoke.saas.repository.core.AjaxResult;
import com.haixiaoke.saas.repository.service.ISubscribeService;
import com.haixiaoke.saas.repository.vo.SubscribeVo;
import com.mzj.saas.util.SignUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 订阅消息Controller
 *
 * @author qixi
 * @date 2023-05-05
 */
@RestController
@RequestMapping("/subscribe")
@Slf4j
public class SubscribeController {

    @Autowired
    private ISubscribeService subscribeService;


    /**
     * 发送订阅消息
     *
     * @return
     */
    @GetMapping("/send")
    public AjaxResult send(SubscribeVo subscribeVo) {
        return subscribeService.send(subscribeVo);
    }

    /**
     * 发送订单消息
     *
     * @return
     */
    @GetMapping("/sendOrder/{orderId}")
    public AjaxResult sendOrder(@PathVariable String orderId) {
        return subscribeService.sendOrder(orderId);
    }

    /**
     * 发送房屋退租消息
     *
     * @return
     */
    @GetMapping("/sendHouseState/{orderId}")
    public AjaxResult sendHouseState(@PathVariable String orderId) {
        return subscribeService.sendHouseState(orderId);
    }


    /**
     * 获取订阅消息 accessToken
     *
     * @return
     */
    public String getAccessToken(Integer flag) {
        return subscribeService.getAccessToken(flag);
    }


    @GetMapping("/sendBillMsg")
    public AjaxResult sendBillMsg(){
        return subscribeService.sendBillMsg(null);
    }



    @GetMapping( "/serial")
    public String checkSign(HttpServletRequest request, HttpServletResponse response) {
        try {
            String signature = request.getParameter("signature");
            String timestamp = request.getParameter("timestamp");
            String nonce = request.getParameter("nonce");
            String echostr = request.getParameter("echostr");
            log.info("本身" + signature);
            if (SignUtil.checkSignature(signature, timestamp, nonce)) {
                return echostr;
            }
        } catch (Exception e) {
            log.error("验证公众号token失败", e);
        }
        return null;
    }


}



